import sys;
import hashlib;
import json;
import os


def print_g(info):
    """
    打印断点调试数据
    """
    print(info);
    sys.exit();


def sha1(str):
    """
    获取字符串散列值
    """
    sha1 = hashlib.sha1();
    sha1.update(str.encode('utf-8'));
    return sha1.hexdigest();


def get_file_cache(key):
    """获取文件缓存"""
    try:
        data = None;
        with open('../cache/' + key + '.json', 'r', encoding='utf-8') as handle:
            result = handle.read();
            handle.close();
            result = result.strip();
            if (len(result) != 0):
                data = json.loads(result);
    except IOError as E:
        data = None;
    return data


def set_file_cache(key, data={}):
    """设置文件缓存"""
    data = json.dumps(data, ensure_ascii=False);
    if (len(data.strip()) == 0):
        return False;
    try:
        with open('../cache/' + key + '.json', 'a', encoding='utf-8') as handle:
            handle.write(data);
            handle.close();
        result = True;
    except IOError as E:
        result = False;
    return result


def mkdir(path):
    # 引入模块
    # 去除首位空格
    path = path.strip()
    # 去除尾部 \ 符号
    path = path.rstrip("\\")

    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    isExists = os.path.exists(path)

    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        # 创建目录操作函数
        os.makedirs(path)
    return True
